﻿using System.Threading.Tasks;
using System.Windows.Forms;
using DevelopersTestPad.AppFrame;

namespace DevelopersTestPad.Worksheets
{
    /// <summary>
    /// A worksheet view.
    /// </summary>
    internal partial class WorksheetView : UserControl
    {
        private bool _isActive;

        public WorksheetView()
        {
            InitializeComponent();

            AppContext.Current.ExecuteRequest += ExecuteRequest;                
        }

        /// <summary>
        /// The worksheet.
        /// </summary>
        public virtual Worksheet Worksheet { set; get; }

        /// <summary>
        /// Initializes the worksheet view.
        /// </summary>
        public virtual void Initialize()
        {            
        }

        /// <summary>
        /// Activates the worksheet view.
        /// </summary>
        public virtual void Activate()
        {
            _isActive = true;
        }

        /// <summary>
        /// Passivates the worksheet view.
        /// </summary>
        public virtual void Passivate()
        {
            _isActive = false;
        }

        /// <summary>
        /// Executes the worksheet on the worksheet view.
        /// </summary>
        protected virtual void Execute()
        {
            
        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        protected virtual void OnDisposing()
        {
        }

        /// <summary>
        /// Handles the Execute request.
        /// </summary>
        private void ExecuteRequest(object sender, AppCommandEventArgs e)
        {
            if (e.Handled == false && _isActive)
            {
                Execute();
                e.Handled = true;
            }
        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                OnDisposing();
            }

            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
